package com.base.self.controller;

import com.base.log.LoginAn;
import com.base.result.ResponseResult;
import com.base.self.dto.selfOrder.DeliverOrderDTO;
import com.base.self.dto.selfOrder.SelfOrderExportDTO;
import com.base.self.dto.selfOrder.SelfOrderTotalListDTO;
import com.base.self.dto.selfOrder.SelfOrderWebListDTO;
import com.base.self.service.ISelfOrderService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;

/**
 * <p>
 * 商品订单 前端控制器
 * </p>
 *
 * @author XiFYuW
 * @since 2020-11-18
 */
@RestController
@RequestMapping("/admin/selfOrder")
public class SelfOrderController {

    private final ISelfOrderService iSelfOrderService;

    public SelfOrderController(ISelfOrderService iSelfOrderService) {
        this.iSelfOrderService = iSelfOrderService;
    }

    @GetMapping(value = "/selfOrderIndex")
    @LoginAn
    public ModelAndView selfOrderIndex() {
        return new ModelAndView("self/index_self_order");
    }

    @GetMapping(value = "/selfOrderTotalIndex")
    @LoginAn
    public ModelAndView selfOrderTotalIndex(HttpServletRequest request, String shopId) {
        request.setAttribute("selfOrderTotalShopId", shopId);
        return new ModelAndView("self/index_self_order_total");
    }

    @PostMapping(value = "/selfOrderTotalList")
    @LoginAn(isRe = true)
    public ResponseResult selfOrderTotalList(@Validated final SelfOrderTotalListDTO selfOrderTotalListDTO) {
        return iSelfOrderService.selfOrderTotalList(selfOrderTotalListDTO);
    }

    @PostMapping(value = "/selfOrderWebList")
    @Validated
    @LoginAn(isRe = true)
    public ResponseResult selfOrderWebList(@Valid final SelfOrderWebListDTO selfOrderWebListDTO) {
        return iSelfOrderService.selfOrderWebList(selfOrderWebListDTO);
    }

    @PostMapping(value = "/deliverOrder")
    @Validated
    @LoginAn(isRe = true)
    public ResponseResult deliverOrder(@Valid final DeliverOrderDTO deliverOrderDTO) {
        return iSelfOrderService.deliverOrder(deliverOrderDTO);
    }

    @PostMapping(value = "/selfOrderExport", produces = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8")
    @Validated
    @LoginAn(isRe = true)
    public void selfOrderExport(HttpServletResponse response, @Valid final SelfOrderExportDTO selfOrderExportDTO) throws IOException {
        iSelfOrderService.selfOrderExport(response, selfOrderExportDTO);
    }

}

